<template>
    <div class="container">
        <h2>牛逼后台管理系统</h2>
        <el-form ref="ruleFormRef" :model="ruleForm" status-icon :rules="rules" class="demo-ruleForm">
            <el-form-item prop="name">
                <el-input v-model="ruleForm.name" placeholder="请输入账号" />
            </el-form-item>
            <el-form-item prop="password">
                <el-input v-model="ruleForm.password" type="password" placeholder="请输入密码" />
            </el-form-item>
            <el-form-item>
                <el-button type="primary" @click="submitForm(ruleFormRef)">登录</el-button>
            </el-form-item>
        </el-form>
    </div>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import type { FormInstance, FormRules } from 'element-plus'
import * as api from '../request/api'
import Cookies from 'js-cookie'
import { useRouter } from 'vue-router'
import { useMenusStroe } from '../stores/menus'
let router = useRouter()   // 路由实例
console.log(router);


const ruleFormRef = ref<FormInstance>()

const checkPass = (rule: any, value: any, callback: any) => {
    // console.log(value);
    if (!value) {
        return callback(new Error('请输入密码'))
    }
    setTimeout(() => {
        // console.log(value.length);
        if (value.length < 1 || value.length > 14) {
            callback(new Error('密码长度需为4-14位'))
        } else {
            callback()
        }
    }, 500)
}

const validateName = (rule: any, value: any, callback: any) => {
    if (value === '') {
        callback(new Error('请输入用户名'))
    } else {
        if (ruleForm.name !== '') {
            if (!ruleFormRef.value) return
            ruleFormRef.value.validateField('checkPass', () => null)
        }
        callback()
    }
}


const ruleForm = reactive({
    name: "wolfcodeLiu",
    password: "666888"
})

const rules = reactive<FormRules>({
    name: [{ validator: validateName, trigger: 'blur' }],
    password: [{ validator: checkPass, trigger: 'blur' }],
})

const submitForm = (formEl: FormInstance | undefined) => {
    if (!formEl) return
    // console.log("formEl", formEl);
    formEl.validate(async (valid) => {
        console.log(valid);
        if (valid) {
            // console.log('submit!')
            // 登录请求的发送
            let result = await api.admin.adminLogin({
                password: ruleForm.password,
                username: ruleForm.name
            });
            // console.log(result.token);
            // 存 localStorage sessStorage cookie
            Cookies.set("token", result.tokenHead + result.token)

            // 可以删除
            // 获取用户信息
            // let adminInfo = await api.admin.getAdminInfo()
            // console.log('adminInfo', adminInfo);

            // 将菜单栏存到 pinia
            // useMenusStroe().updateMenus(adminInfo.menus)

            // 跳转到首页
            // console.log(router);

            router.push('/index')

        } else {
            console.log('error submit!')
            return false
        }
    })
}

</script>

<style lang="scss" scoped>
.container {
    width: 100%;
    height: 100vh;
    background-color: #223344;
    display: flex;
    justify-content: center;
    align-items: center;
    color: #fff;
    flex-direction: column;

    h2 {
        color: #fff;
        font-size: 28px;
        font-weight: 600;
        line-height: 50px;
        margin-bottom: 20px;
    }

    .el-form-item {
        width: 360px;

        .el-input {
            height: 50px;
            line-height: 50px;

            // 使用 :deep 修改第三方组件库的样式名 来修改样式
            :deep .el-input__wrapper {
                background-color: rgba(0, 0, 0, 0.1);
                // H 色相 S 饱和度 L 亮度 A 透明度
                box-shadow: 0 0 0 1px hsla(0, 0%, 100%, 0.1) inset;

                .el-input__inner {
                    font-size: 18px;
                    color: #fff;
                }
            }

        }

        .el-button {
            width: 100%;
            height: 50px;
            font-size: 18px;
        }
    }


}
</style>